charset="UTF-8"
Ext.onReady(function(){
	Ext.QuickTips.init();
     // example of custom renderer function
    function change(val){
        if(val > 0){
            return '<span style="color:green;">' + val + '</span>';
        }else if(val < 0){
            return '<span style="color:red;">' + val + '</span>';
        }
        return val;
    }

    // example of custom renderer function
    function pctChange(val){
        if(val > 0){
            return '<span style="color:green;">' + val + '%</span>';
        }else if(val < 0){
            return '<span style="color:red;">' + val + '%</span>';
        }
        return val;
    }
    
    var hospitalRecord = Ext.data.Record.create([
				{name:'id'},  
              {name:'name'},  
              {name:'admin'},  
              {name:'setupdate',type : 'date', dateFormat : 'Y-m-d' },  
              {name:'introduction'}  
		]);
    // create the data store
    var hospitalStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({  
                	url:"queryAllHospitalByPage.action",  
                    method:"post"  
                }),  
        reader : new Ext.data.JsonReader({
        	totalProperty : 'totalProperty',
        	root : 'hospitalVOList'
        	 }, 
        	 hospitalRecord
        	 )
    });

    //查询科室面板
    var queryDepartmentForm = new Ext.FormPanel({
        labelAlign: 'top',
        frame:true,
        title: '查询条件',
        bodyStyle:'padding:5px 5px 0',
        width:780,
        collapsible: true,//最小化
        items: [{
        		columnWidth:.2,
            	layout:'form',
            	items:[{
					xtype:"combo",
					name: "hospitalName",
					store:hospitalStore,
					mode:"remote",
					editable:false,
					pageSize: 10,
					displayField:"name",
					hiddenName: "hospitalId",
					valueField:"id",
					triggerAction: 'all',
					fieldLabel:"所属医院",
					emptyText:"请选择医院",
					loadingText:"正在加载数据，请稍后。。。",
					maxLength:20,
                    maxLengthText:'最大长度20',
                    allowBlank:false,
                    blankText:'不能为空'
            	}]
			},{
            layout:'column',
            items:[{
                columnWidth:.2,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '科室名称',
                    id:'queryname',
                    name: 'name',
                    anchor:'90%'
                }]
            },{
                columnWidth:.2,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '管理员',
                    id:'queryadmin',
                    name: 'admin',
                    anchor:'90%'
                }]
            },{
            	columnWidth:.3,
                layout: 'form',
                items: [{
      			    fieldLabel: '起始日期',
        			name: 'startsetupdate',
        			id: 'querystartdt',
        			vtype: 'daterange',
        			xtype:'datefield',
        			invalidText : "{0} 是无效的日期 - 必须符合格式： {1}", 
					format : "Y-m-d" ,
					maxValue:new Date(),  
          			maxText:'时间不能超过今天',  
                    anchor:'90%',
        			endDateField: 'queryenddt' // id of the end date field
      			}]
            },{
                columnWidth:.3,
                layout: 'form',
                items: [{
        			fieldLabel: '结束日期',
        			name: 'endsetupdate',
        			id: 'queryenddt',
        			vtype: 'daterange',
        			xtype:'datefield',
        			invalidText : "{0} 是无效的日期 - 必须符合格式： {1}", 
					format : "Y-m-d" ,
					maxValue:new Date(),  
          			maxText:'时间不能超过今天',  
                    anchor:'90%',
        			startDateField: 'querystartdt' // id of the start date field
      			}]
            }]
        }],
        
		monitorValid:true,
		
        buttons: [{
            text: '查询',
            formBind: true,
            handler:function(){       
            	departmentStore.baseParams = {
            		hospitalId:queryDepartmentForm.getForm().findField('hospitalId').getValue(),
        			name:queryDepartmentForm.getForm().findField('queryname').getValue(),
                	admin:queryDepartmentForm.getForm().findField('queryadmin').getValue(),
                	startsetupdate:queryDepartmentForm.getForm().findField('querystartdt').getValue(),
               		endsetupdate:queryDepartmentForm.getForm().findField('queryenddt').getValue()
        		},
                departmentStore.reload({params:{start:0,limit:15}});
                departmentGrid.addBtn.setDisabled(false);
                }
        },{
            text: '重置',
            handler:function(){
					queryDepartmentForm.form.reset();
					departmentGrid.addBtn.setDisabled(true);
					departmentStore.removeAll();
                }
        }]
    });
    

	//添加科室面板
    var addDepartmentForm = new Ext.FormPanel({
        labelAlign: 'top',
        frame:true,
        title: '科室信息',
        bodyStyle:'padding:5px 5px 0',
        width: 600,
        items: [{
            layout:'column',
            items:[{
                columnWidth:.3,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    allowBlank:false,
                    blankText:'不能为空',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '科室名称',
                    id:'addname',
                    name: 'name',
                    anchor:'90%'
                },{
                 	inputType: 'password',
                	xtype:'textfield',
                    allowBlank:false,
                    blankText:'不能为空',
                    minLength:4,
                    minLengthText:'最小长度4',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '密码',
                    id: 'addpassword',
                    name: 'password',
                    anchor:'90%'
                }]
            },{
                columnWidth:.3,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    allowBlank:false,
                    blankText:'不能为空',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '管理员',
                    id:'addadmin',
                    name: 'admin',
                    anchor:'90%'
                },{
                  	inputType: 'password',
                	xtype:'textfield',
                    vtype:'password',
                    initialPassField: 'addpassword',
                    allowBlank:false,
                    blankText:'不能为空',
                    minLength:4,
                    minLengthText:'最小长度4',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '重复密码',
                    id:'addconfirmpassword',
                    name: 'confirmpassword',
                    anchor:'90%'
                }]
            },{
            	columnWidth:.3,
                layout: 'form',
                items: [{
                    xtype:'datefield',
                    fieldLabel: '成立日期',
                    id:'addsetupdate',
                    name: 'setupdate',
                    allowBlank:false,
                    blankText:'不能为空',
                    emptyText:'请选择日期', 
                    invalidText : "{0} 是无效的日期 - 必须符合格式： {1}", 
					format : "Y-m-d" ,
					maxValue:new Date(),  
          			maxText:'时间不能超过今天',  
                    anchor:'100%'
                },{
                    xtype:'textfield',
                    hidden:true,
                    id:'hospitalId',
                    name: 'hospitalId',
                    anchor:'90%'
                }]
            }]
        },{
        	name: 'introduction',
            xtype:'htmleditor',
            maxLength:1000,
            maxLengthText:'最大长度1000',
            id:'addintroduction',
            fieldLabel:'简介',
            height:200,
            anchor:'98%'
        }],
		monitorValid:true,
		
        buttons: [{
            text: '保存',
            formBind: true,
            handler:function(){                   
            	addDepartmentForm.getForm().submit({//客户端的数据提交给服务器   
                     url:'addDepartment.action',      
                     method:'post',
                     waitTitle: '信息', 
                    waitMsg:"正在提交，请稍候。。。",  
                    success: function(form, action){   
                         Ext.MessageBox.alert('信息', '添加成功');
                         addDepartmentForm.form.reset();
                 		 addDepartmentWindow.hide();
                 		 departmentStore.reload();
                      },
                     failure:function(form, action){  
                         Ext.MessageBox.alert('错误', '添加失败，请重试'); 
                         addDepartmentForm.form.reset();
                 		 addDepartmentWindow.hide();
                     }                                  
                 }); 
                 
                }
        },{
            text: '取消',
            handler:function(){
					addDepartmentForm.form.reset();
                    addDepartmentWindow.hide();
                }
        }]
    });
	//添加科室窗体
    var addDepartmentWindow = new Ext.Window({
	title : '添加科室',
	width : 615,
	height : 440,
	plain : true,
	closeAction : 'hide',// 关闭窗口
	maximizable : false,// 最大化控制 值为true时可以最大化窗体
	layout:'Form',
	items : [addDepartmentForm]
});
  //修改科室面板
    var updateDepartmentForm = new Ext.FormPanel({
        labelAlign: 'top',
        frame:true,
        title: '科室信息',
        bodyStyle:'padding:5px 5px 0',
        width: 600,
        items: [{
            layout:'column',
            items:[{
                columnWidth:.3,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    allowBlank:false,
                    blankText:'不能为空',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '科室名称',
                    id:'updatename',
                    name: 'name',
                    anchor:'90%'
                },{
                 	inputType: 'password',
                	xtype:'textfield',
                    allowBlank:false,
                    blankText:'不能为空',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '密码',
                    id: 'updatepassword',
                    name: 'password',
                    anchor:'90%'
                }]
            },{
                columnWidth:.3,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    allowBlank:false,
                    blankText:'不能为空',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '管理员',
                    id:'updateadmin',
                    name: 'admin',
                    anchor:'90%'
                },{
                  	inputType: 'password',
                	xtype:'textfield',
                    vtype:'password',
                    initialPassField: 'updatepassword',
                    allowBlank:false,
                    blankText:'不能为空',
                    maxLength:20,
                    maxLengthText:'最大长度20',
                    fieldLabel: '重复密码',
                    id:'updateconfirmpassword',
                    name: 'confirmpassword',
                    anchor:'90%'
                }]
            },{
            	columnWidth:.3,
                layout: 'form',
                items: [{
                    xtype:'datefield',
                    fieldLabel: '成立日期',
                    id:'updatesetupdate',
                    name: 'setupdate',
                    allowBlank:false,
                    blankText:'不能为空',
                    emptyText:'请选择日期', 
                    invalidText : "{0} 是无效的日期 - 必须符合格式： {1}", 
					format : "Y-m-d" ,
					maxValue:new Date(),  
          			maxText:'时间不能超过今天',  
                    anchor:'100%'
                },{
                    xtype:'textfield',
                    hidden:true,
                    id:'id',
                    name: 'id',
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    hidden:true,
                    id:'hospitalId',
                    name: 'hospitalId',
                    anchor:'90%'
                }]
            }]
        },{
        	name: 'introduction',
            xtype:'htmleditor',
            maxLength:1000,
            maxLengthText:'最大长度1000',
            id:'updateintroduction',
            fieldLabel:'简介',
            height:200,
            anchor:'98%'
        }],
		monitorValid:true,
		
        buttons: [{
            text: '保存',
            formBind: true,
            handler:function(){                   
            	updateDepartmentForm.getForm().submit({//客户端的数据提交给服务器   
                     url:'updateDepartment.action',      
                     method:'post',
                     waitTitle: '信息', 
                    waitMsg:"正在提交，请稍候。。。",  
                    success: function(form, action){   
                         Ext.MessageBox.alert('信息', '修改成功');
                         updateDepartmentForm.form.reset();
                 		 updateDepartmentWindow.hide();
                 		 departmentStore.reload();
                      },
                     failure:function(form, action){  
                         Ext.MessageBox.alert('错误', '修改失败'); 
                         updateDepartmentForm.form.reset();
                 		 updateDepartmentWindow.hide();
                     }                                  
                 }); 
                 
                }
        },{
            text: '取消',
            handler:function(){
					updateDepartmentForm.form.reset();
                    updateDepartmentWindow.hide();
                }
        }]
    });
	//修改科室窗体
    var updateDepartmentWindow = new Ext.Window({
	title : '修改科室',
	width : 615,
	height : 440,
	plain : true,
	closeAction : 'hide',// 关闭窗口
	maximizable : false,// 最大化控制 值为true时可以最大化窗体
	layout:'Form',
	items : [updateDepartmentForm]
});
    var departmentRecord = Ext.data.Record.create([
				{name:'id'},  
              {name:'name'},  
              {name:'admin'},  
              {name:'setupdate',type : 'date', dateFormat : 'Y-m-d' },  
              {name:'introduction'}  
		]);
    // create the data store
    var departmentStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({  
                	url:"queryDepartmentByPage.action",  
                    method:"post"  
                }),  
        reader : new Ext.data.JsonReader({
        	totalProperty : 'totalProperty',
        	root : 'departmentVOList'
        	 }, 
        	 departmentRecord
        	 )
    });



	 var sm = new Ext.grid.CheckboxSelectionModel();  
	 
    var column = new Ext.grid.ColumnModel({
    		columns:[
    		new Ext.grid.RowNumberer(),
    		sm,
            {
            	header: "科室编号", 
            	width: 70, 
            	sortable: true, 
            	dataIndex: 'id'
            },
            {
            	header: "科室名称", 
            	width: 200, 
            	sortable: true, 
            	dataIndex: 'name'
            },
            {
            	header: "管理员", 
            	width: 150, 
            	sortable: true,
            	dataIndex: 'admin'
            },
            {
            	header: "成立日期", 
            	width: 150, 
            	sortable: true, 
            	renderer: Ext.util.Format.dateRenderer('Y/m/d'), 
            	dataIndex: 'setupdate'
            },
            {
            	header: "简介", 
            	width: 300,
            	dataIndex: 'introduction'
            }
        ]});
        column.defaultSortable = true;//默认可排序  


        
    // create the Grid
    var departmentGrid = new Ext.grid.GridPanel({
        id:'department-grid',
        store: departmentStore,
        cm: column,
        sm: sm,
        height:400,
        width:780,
        title:'科室信息列表',
		loadMask: {msg:'正在加载数据，请稍侯……'},
		collapsible: true,//最小化
    	viewConfig: {
            forceFit:true//加入该选项全屏显示表格且自适应大小，不产生横向滚动条
         },
        plugins: new Ext.ux.PanelResizer({
            minHeight: 100
        }),
        tbar: [{
        	ref: '../addBtn',
            iconCls: 'icon-add',
            text: '添加科室',
            disabled: true,
            handler: function(){
            	addDepartmentForm.getForm().findField('hospitalId').setValue(queryDepartmentForm.getForm().findField('hospitalId').getValue());
            	addDepartmentWindow.show();

            }
        },{
        	ref: '../updateBtn',
            iconCls: 'icon-update',
            text: '修改科室',
            disabled: true,
            handler: function(){
            	var s = departmentGrid.getSelectionModel().getSelected();
            	updateDepartmentForm.getForm().findField('hospitalId').setValue(queryDepartmentForm.getForm().findField('hospitalId').getValue());
            	updateDepartmentForm.getForm().findField('id').setValue(s.get('id'));
            	updateDepartmentForm.getForm().findField('admin').setValue(s.get('admin'));
            	updateDepartmentForm.getForm().findField('setupdate').setValue(s.get('setupdate'));
            	updateDepartmentForm.getForm().findField('introduction').setValue(s.get('introduction'));
            	updateDepartmentForm.getForm().findField('name').setValue(s.get('name'));
            	updateDepartmentWindow.show();
            }
        },{
        	ref: '../removeBtn',
            iconCls: 'icon-delete',
            text: '删除科室',
            disabled: true,
            handler: function(){
            	Ext.MessageBox.confirm("请确认","是否真的要删除指定的内容",function(button,text){
					if(button=="yes"){
						//执行删除操作
						 var s = departmentGrid.getSelectionModel().getSelections();
                			for(var i = 0, r; r = s[i]; i++){
                    		Ext.Ajax.request({
     							method : 'POST',                             //发送方式.'POST'或'GET',一般是'POST'
     							url: 'deleteDepartment.action',                  //发送到页面
        						success:function(form,action){                   //发送成功的回调函数
       								Ext.Msg.alert('信息','删除成功');        //弹出对话框
       								departmentStore.reload();                      //重新读入数据
      							},
     							failure:function(form,action){                          //发送失败的回调函数
       								Ext.Msg.alert("错误", "删除失败");
      							},
     							params:{id : r.get('id')} //发送参数
    						});

                		}
				
					}
				});

            }
        }],
        bbar: new Ext.PagingToolbar({
            pageSize: 15,
            store: departmentStore,
            displayInfo: true,
			displayMsg:'显示第 {0} 条到 {1} 条记录，一共 {2} 条',  
            emptyMsg:'没有记录',
            plugins: new Ext.ux.ProgressBarPager()
        })
    });
    
    //选中以后激活删除按钮
    departmentGrid.getSelectionModel().on('selectionchange', function(sm){
       
       	departmentGrid.removeBtn.setDisabled(sm.getCount() < 1);
        departmentGrid.updateBtn.setDisabled(sm.getCount() != 1);
    });
	
    
   departmentGrid.render('departmentGrid');
   queryDepartmentForm.render('queryPanel');
   
   
});